package com.sljy.hzk.ui.fragment;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntDef;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.afollestad.materialdialogs.GravityEnum;
import com.afollestad.materialdialogs.MaterialDialog;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.zxing.integration.android.IntentIntegrator;
import com.sljy.hzk.R;
import com.sljy.hzk.entity.TaskDatabaseEntity;
import com.sljy.hzk.entity.TaskEntity;
import com.sljy.hzk.net.HttpHeper;
import com.sljy.hzk.net.service.TaskService;
import com.sljy.hzk.room.AppDataBase;
import com.sljy.hzk.room.dao.TaskDao;
import com.sljy.hzk.ui.QrCodeActivity;
import com.sljy.hzk.ui.fragment.lazyfragment.LazyFragment;
import com.sljy.hzk.ui.task.HiddenDangerActivity__JumpCenter;
import com.sljy.hzk.ui.task.TaskExecuteActivity;
import com.sljy.hzk.ui.task.TaskExecuteActivity__JumpCenter;
import com.sljy.hzk.ui.task.TaskMainActivity;
import com.sljy.hzk.util.EventListenerManager;
import com.sljy.hzk.view.MarginItemDecoration;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.EventListener;
import java.util.List;

import butterknife.BindDrawable;
import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created  on 2018/7/2 0002
 *
 * @author 唐开阔
 * @describe 任务列表fragment
 */
public class TaskListFragment extends LazyFragment {
    public static final int TASK_UNFINSH = 1;
    public static final int TASK_TIME_OVER = 2;
    public static final int TASK_FINSHED = 3;
    @BindView(R.id.recycer)
    RecyclerView recycerView;
    @BindDrawable(R.drawable.shape_task_bg)
    Drawable drawableFinsh;
    @BindDrawable(R.drawable.shape_task_bg)
    Drawable drawableNoFinsh;
    @BindDrawable(R.drawable.shape_task_bg)
    Drawable drawableNoUp;
    @BindDrawable(R.drawable.shape_task_bg)
    Drawable drawableTimeOver;
    BaseQuickAdapter adapter;
    public List<TaskEntity> datas = new ArrayList<>();
    public TaskDao taskDao;
    public int type;

    @IntDef({TASK_UNFINSH, TASK_TIME_OVER, TASK_FINSHED})
    @Target(ElementType.PARAMETER)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface TaskType {
    }

    public static TaskListFragment get(@TaskType int type, List<TaskEntity> datas) {
        TaskListFragment taskFragment = new TaskListFragment();
        taskFragment.type = type;
        if (datas != null && datas.size() != 0) {
            taskFragment.datas.addAll(datas);
        }
        return taskFragment;
    }

    public void notify(List<TaskEntity> data) {
        datas.clear();
        datas.addAll(data);
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected int getResId() {
        return R.layout.fragment_task_list;
    }

    @Override
    protected void onRealViewLoaded(View view) {
//        taskDao = AppDataBase.getInstance().getTaskDao();
//        recycerView.setLayoutManager(new LinearLayoutManager(getContext()));
//        recycerView.addItemDecoration(new MarginItemDecoration(20));
//
//        adapter = new BaseQuickAdapter<TaskEntity, BaseViewHolder>(R.layout.item_task, datas) {
//            @Override
//            protected void convert(BaseViewHolder helper, final TaskEntity item) {
//                boolean isHaveFromLocal = !TextUtils.isEmpty(taskDao.get(item.getWorkPlanId()));
//                helper.setText(R.id.tv_name, item.getTitle());
//                helper.setText(R.id.tv_num, item.getNum());
//                helper.setText(R.id.tv_pos, item.getPartition());
//                helper.setText(R.id.tv_time, item.getHours());
//                helper.setText(R.id.tv_do, isHaveFromLocal ? "再次编辑" : "执行");
//                View localHintText = helper.getView(R.id.tv_local);
//                View time = helper.getView(R.id.tv_time);
//                if (isHaveFromLocal) {
//                    time.setBackground(drawableNoUp);
//                } else {
//                    switch (type){
//                        case TASK_UNFINSH:
//                            time.setBackground(drawableNoFinsh);
//                            break;
//                        case TASK_TIME_OVER:
//                           // helper.getView(R.id.tv_do).setVisibility(View.INVISIBLE);
//                            time.setBackground(drawableTimeOver);
//                            break;
//                        case TASK_FINSHED:
//                           // helper.getView(R.id.tv_do).setVisibility(View.INVISIBLE);
//                            time.setBackground(drawableFinsh);
//                            break;
//                    }
//                }
//                localHintText.setVisibility(isHaveFromLocal ? View.VISIBLE : View.GONE);
//                helper.getView(R.id.img_danger).setVisibility(item.isDanger() ? View.VISIBLE : View.GONE);
//                helper.getView(R.id.tv_do).setOnClickListener(v -> {
//                      //  if (type == TASK_UNFINSH || (type == TASK_TIME_OVER && item.getFlag() == 1) ||item.isDanger()) {
//                    if (type == TASK_UNFINSH ||item.isDanger() || type == TASK_FINSHED || type == TASK_TIME_OVER) {
//                            //判断是否上锁
//                            if (item.getAppSwitch() == 0 & !item.isDanger()) {
//                                //打开扫码
//                                try {
//                                    EventListenerManager.Instance().sendEvent("DoTaskCallBack",item.getWorkPlanId(), isHaveFromLocal, true);
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//                                return;
//                            }
//                            if (item.isDanger()){
//                                HiddenDangerActivity__JumpCenter
//                                        .builder(getActivity())
//                                        .setDangerId(item.getDangerId())
//                                        .create()
//                                        .go();
//                            }else {
//                                TaskExecuteActivity__JumpCenter
//                                        .builder(getActivity())
//                                        .setIsFromLocal(isHaveFromLocal)
//                                        .setWorkPlanId(item.getWorkPlanId())
//                                        .setWorkType(type)
//                                        .create()
//                                        .go();
//                            }
//                        }else {
//                            Toast.makeText(getActivity(), "不可执行", Toast.LENGTH_SHORT).show();
//                        }
//                    });
//
//                helper.getView(R.id.tv_look_user).setOnClickListener(v -> {
//                    List<String> items = new ArrayList<>();
//                    for (TaskEntity.PersonsBean personsBean : item.getPersons()) {
//                        items.add(personsBean.getName() + "  " + personsBean.getTel());
//                    }
//                    if (items.size() != 0) {
//                        new MaterialDialog.Builder(getActivity()).title("人员查看")
//                                .titleGravity(GravityEnum.CENTER)
//                                .titleColor(Color.parseColor("#000000"))
//                                .autoDismiss(true)
//                                .items(items).build().show();
//                    }
//                });
//            }
//        };
//        recycerView.setAdapter(adapter);
    }
}
